﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Summary description for FileProvider
/// CuongBH
/// 07/12/2011
/// </summary>
namespace EHTWebsite.Code
{
    public class FileProvider
    {
        public static Int32 IUFile(File theFile, bool IsAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IUFiles";
            command.CommandType = CommandType.StoredProcedure;

            SqlParameter theDanhMucSanPhamParameter = new SqlParameter();

            theDanhMucSanPhamParameter.ParameterName = "@FileIDOut";
            theDanhMucSanPhamParameter.Direction = ParameterDirection.Output;
            theDanhMucSanPhamParameter.DbType = DbType.Int32;

            command.Parameters.Add(theDanhMucSanPhamParameter);

            command.Parameters.Add(new SqlParameter("@FileID", theFile.FileID));
            command.Parameters.Add(new SqlParameter("@FileUrl", theFile.FileUrl));
            command.Parameters.Add(new SqlParameter("@MoTaVN", theFile.MoTaVN));
            command.Parameters.Add(new SqlParameter("@MoTaOther", theFile.MoTaOther));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", theFile.SapXep));
            command.Parameters.Add(new SqlParameter("@Publish", theFile.TrangThai));
            command.Parameters.Add(new SqlParameter("@IsAdd", IsAdd));

            DataProvider.ExecuteNoneQuery(command);
            return Int32.Parse(command.Parameters["@FileIDOut"].Value.ToString());
        }

        public static void DeleteFileByFileID(Int32 FileID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteFileByFileID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@FileID", FileID));

            DataProvider.ExecuteNoneQuery(command);
        }

        public static DataSet GetFiles(Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_GetFilesForBackEnd]";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));

            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
            return ds;
        }
        public static DataSet GetFilesForFrontEnd(Boolean isVN) 
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetFilesForFrontEnd";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@IsVN",isVN));
            DataSet ds= DataProvider.ExecuteDataSet(command);
            return ds;
        }

        public static DataSet GetAllFiles(Boolean isVN) 
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetAllFiles";
            command.CommandType = CommandType.StoredProcedure;           
            DataSet ds = DataProvider.ExecuteDataSet(command);
            return ds;
        }
        public static File GetFileByFileID(Int32 FileID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetFileByFileID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@FileID", FileID));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            File theFile = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theFile = new File();
                theFile.FileUrl = ds.Tables[0].Rows[0]["FileUrl"].ToString();
                theFile.MoTaVN = ds.Tables[0].Rows[0]["MoTaVN"].ToString();
                theFile.MoTaOther = ds.Tables[0].Rows[0]["MoTaOther"].ToString();
                theFile.FileID = Int32.Parse(ds.Tables[0].Rows[0]["FileID"].ToString());
            }

            return theFile;
        }

        public static void ChangePublish(Int32 FileID, Boolean TrangThai)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_ChangePublishByFileID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@FileID", FileID));
            command.Parameters.Add(new SqlParameter("@Publish", TrangThai));
            DataProvider.ExecuteNoneQuery(command);
        }
    }
}